package cn.doitedu.jvm.mem;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * VM Args:-Xmx20M -XX:MaxDirectMemorySize=10M
 *
 * Exception in thread "main" java.lang.OutOfMemoryError
 * 	at sun.misc.Unsafe.allocateMemory(Native Method)
 * 	at cn.doitedu.jvm.mem.DirectMemDemo.main(DirectMemDemo.java:18)
 */
public class DirectMemDemo {
    private static long _1M = 1024 * 1024;

    public static void main(String[] args) throws IllegalAccessException {
        Field declaredField = Unsafe.class.getDeclaredFields()[0];
        declaredField.setAccessible(true);
        Unsafe unsafe = (Unsafe) declaredField.get(null);
        while(true) {
            unsafe.allocateMemory(_1M);
        }

    }
}
